package com.hd.hdxgddtools.mybatis.config;

import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import lombok.Data;

@Data
public class AutoGenerator {
    private DataSourceConfig dataSource;
    private StrategyConfig strategy;
    private PackageConfig packageInfo;
    private TemplateConfig template;
    private GlobalConfig globalConfig;
    private String templateEngineType;
    private InjectionConfig injectionConfig;

    public com.baomidou.mybatisplus.generator.AutoGenerator toAutoGenerator(String project) {
        com.baomidou.mybatisplus.generator.AutoGenerator generator = new com.baomidou.mybatisplus.generator.AutoGenerator();
        if (this.dataSource != null)
            generator.setDataSource(this.dataSource.toDataSourceConfig());
        if (this.strategy != null)
            generator.setStrategy(this.strategy.toStrategyConfig());
        if (this.packageInfo != null)
            generator.setPackageInfo(this.packageInfo.toPackageInfo());
        if (this.template != null)
            generator.setTemplate(this.template.toTemplateConfig());
        if (this.globalConfig != null)
            generator.setGlobalConfig(this.globalConfig.toGlobalConfig(project));
        if (templateEngineType != null && templateEngineType.contains("velocity")) {
            generator.setTemplateEngine(new VelocityTemplateEngine());
        } else {
            generator.setTemplateEngine(new FreemarkerTemplateEngine());
        }
        if (this.injectionConfig != null)
            generator.setCfg(this.injectionConfig.toCfg(project, packageInfo.getModuleName()));
        return generator;
    }
}
